package com.hrt.datasender.process.strategy.hj212;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.btcode.db.result.Row;
import com.btcode.orm.conver.DefaultModelConver;
import com.hrt.datasender.datacollect.drive.weigeng.BdDoorAccessModel;
import com.hrt.datasender.datadao.model.BcSendPlatformModel;
import com.hrt.datasender.datadao.service.BCSiteService;
import com.hrt.datasender.process.indentity.hj212.MessageModel;
import com.hrt.datasender.process.strategy.IProcessStrategy;
import com.hrt.datasender.process.strategy.hj212.util.CRCUtil;

public class Com2099Process implements IProcessStrategy {
    
    @Override
    public String getStrategyName() {
        return "门禁数据解析";
    }

    @Override
    public String getCommandId() {
        return "2099";
    }

    @Override
    public Object packet(List<Row> data, BcSendPlatformModel platform) throws Exception {
        String result = getCommandPacket(platform, data);
        return result;
    }

    @Override
    public Object packetReissue(List<Row> data, BcSendPlatformModel platform) throws Exception {
        String result = getCommandPacket(platform, data);
        return result;
    }

    @Override
    public void decode(MessageModel data, BcSendPlatformModel sendPlatform) throws Exception {
        // TODO Auto-generated method stub

    }

    public String getCommandPacket(BcSendPlatformModel sendPlatform, List<Row> data) {

        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmsss");

        DefaultModelConver modelConver = new DefaultModelConver();

        BdDoorAccessModel model = modelConver.converORMModel(data.get(0), BdDoorAccessModel.class);
        String cp = "CarNo=" + model.getCarNo() + ";";
        cp += "ControlNo=" + model.getControlNo() + ";";
        cp += "DoorNo=" + model.getDoorNo() + ";";
        cp += "InOrOut=" + model.getInOrOut() + ";";
        cp += "RecordDatetime=" + model.getRecordDatetime() + ";";
        cp += "RecordResult=" + model.getRecordResult() + ";";
        cp += "RecordType=" + model.getRecordType() + ";";
        cp += "RecordReason=" + model.getRecordReason() + ";";
        cp += "doorMagnetism=" + model.getdoorMagnetism() + ";";
        StringBuilder strBuilder = new StringBuilder();

        strBuilder.append("QN=" + sf.format(new Date()) + ";");
        strBuilder.append("ST=" + BCSiteService.getInstance().getModel().getSystemType() + ";");
        strBuilder.append("CN=2099;");
        strBuilder.append("PW=123456;");
        strBuilder.append("MN=" + sendPlatform.getMn() + ";");
        strBuilder.append("CP=&&" + cp + "&&;");

        String crcStr = CRCUtil.calcCRC(strBuilder.toString());

        String dataLength = strBuilder.length() + "";
        final int dataLengthFormat = 4;
        while (dataLength.length() < dataLengthFormat) {
            dataLength = "0" + dataLength;
        }

        strBuilder.insert(0, "##" + dataLength);

        strBuilder.append(crcStr + "\r\n");
        return strBuilder.toString();
    }

}
